package mx.itesm.manejadores;

import java.io.IOException;

import mx.itesm.constants.ConstantesManejadores;
import mx.itesm.ninja.GameActivity;

import org.andengine.audio.music.Music;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.bitmap.AssetBitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.util.Log;


public class ManejadorRecursos {

	/* 
	 * **************************************************************
	 *                       Variables del manejador de recursos
	 * **************************************************************
	 */

	private static final ManejadorRecursos INSTANCIA = new ManejadorRecursos();

	//Variables del contexto actual del juego
	public Engine engine;
	public GameActivity gActivity;
	public Camera camara;
	public VertexBufferObjectManager mVerticesObjetos;

	/* Splash Screen */

	public ITextureRegion splashRegion;
	private BitmapTextureAtlas splashTextureAtlas;
	private static final int ANCHO_LOGO = ConstantesManejadores.ANCHO_LOGO;
	private static final int ALTO_LOGO = ConstantesManejadores.ALTO_LOGO;

	//Constantes del tamanio de los menus
	private static final int ANCHO_MENU = ConstantesManejadores.ANCHO_MENU;
	private static final int ALTO_MENU = ConstantesManejadores.ALTO_MENU;

	/* Logo Screen */
	
	public ITextureRegion logoJuegoRegion;
	private BitmapTextureAtlas logoJuegoTextureAtlas;
	
	/* Main Menu Screen */

	//Menu Screen
	public ITextureRegion mainMenuRegion;
	private BitmapTextureAtlas mainMenuTextureAtlas;

	//Constantes Botones
	private static final int ANCHO_BOTON = ConstantesManejadores.ANCHO_BOTON;
	private static final int ALTO_BOTON = ConstantesManejadores.ALTO_BOTON;
	private static final int ANCHO_BOTON_AKANE = ConstantesManejadores.ANCHO_AKANE;
	private static final int ALTO_BOTON_AKANE = ConstantesManejadores.ALTO_AKANE;
	private static final int ANCHO_BOTON_KAZUKI = ConstantesManejadores.ANCHO_KAZUKI;
	private static final int ALTO_BOTON_KAZUKI = ConstantesManejadores.ALTO_KAZUKI;
	private static final int BOTON_NIVELX = ConstantesManejadores.BOTON_NIVELX;

	//Boton About
	public ITextureRegion botonAboutRegion;
	private BitmapTextureAtlas botonAboutTextureAtlas;

	//Boton Play
	public ITextureRegion botonPlayRegion;
	private BitmapTextureAtlas botonPlayTextureAtlas;

	//Boton Settings
	public ITextureRegion botonSettingsRegion;
	private BitmapTextureAtlas botonSettingsTextureAtlas;

	//Boton Scores
	public ITextureRegion botonScoresRegion;
	private BitmapTextureAtlas botonScoresTextureAtlas;

	//Boton Instructions
	public ITextureRegion botonInstrucRegion;
	private BitmapTextureAtlas botonInstrucTextureAtlas;

	/* About Screen */

	public ITextureRegion aboutRegion;
	private BitmapTextureAtlas aboutTextureAtlas;

	/* Instructions Screen */

	public ITextureRegion instrucRegion;
	private BitmapTextureAtlas instrucTextureAtlas;

	/* Scores Screen */

	public ITextureRegion scoresRegion;
	private BitmapTextureAtlas scoresTextureAtlas;

	/* Settings Screen */

	//La pantalla en si
	public ITextureRegion settingsRegion;
	private BitmapTextureAtlas settingsTextureAtlas;

	//Los botones de musica y sonidos
	public TiledTextureRegion botonMusicaRegion;
	private BuildableBitmapTextureAtlas botonMusicaTextureAtlas; // Musica	
	public TiledTextureRegion botonSonidosRegion;
	private BuildableBitmapTextureAtlas botonSonidosTextureAtlas; // Sonido
	

	/* Loading Screen */

	public ITextureRegion loadingRegion;
	private BitmapTextureAtlas loadingTextureAtlas;

	/* Elegir Personaje */

	//Fondo
	public ITextureRegion elegirPersonajeRegion;
	private BitmapTextureAtlas elegirPersonajeTextureAtlas;

	//Botones de Personajes
	public ITextureRegion botonAkaneRegion;
	private BitmapTextureAtlas botonAkaneTextureAtlas;
	public ITextureRegion botonKazukiRegion;
	private BitmapTextureAtlas botonKazukiTextureAtlas;

	/* Elegir Nivel */

	//Fondo
	public ITextureRegion elegirNivelRegion;
	private BitmapTextureAtlas elegirNivelTextureAtlas;

	//Botones de Elegir Nivel
	public ITextureRegion botonNivelUnoRegion;
	private BitmapTextureAtlas botonNivelUnoTextureAtlas;
	public ITextureRegion botonNivelDosRegion;
	private BitmapTextureAtlas botonNivelDosTextureAtlas;
	public ITextureRegion botonNivelTresRegion;
	private BitmapTextureAtlas botonNivelTresTextureAtlas;
	
	//Boton bloquear nivel
	public ITextureRegion botonBloquearNivelRegion;
	private BitmapTextureAtlas botonBloquearNivelAtlas;

	/* Ganaste */
	
	public ITextureRegion fondoGanasteRegion;
	private BitmapTextureAtlas fondoGanasteTextureAtlas;

	/* Perdiste */
	
	public ITextureRegion fondoPerdisteRegion;
	private BitmapTextureAtlas fondoPerdisteTextureAtlas;

	/* Pausa */
	
	public ITextureRegion fondoPausaRegion;
	private BitmapTextureAtlas fondoPausaTextureAtlas;

	/* Botones diversos */

	// Menu
	public ITextureRegion botonMenuRegion;
	private BitmapTextureAtlas botonMenuTextureAtlas;

	// Volver a Jugar
	public ITextureRegion botonVolverJugarRegion;
	private BitmapTextureAtlas botonVolverJugarTextureAtlas;

	// Siguiente Nivel
	public ITextureRegion botonSigNivelRegion;
	private BitmapTextureAtlas botonSigNivelTextureAtlas;
	
	// Reanudar
	public ITextureRegion botonReanudarRegion;
	private BitmapTextureAtlas botonReanudarTextureAtlas;
	
	/* Personajes */
	
	public TiledTextureRegion kazukiJuegoRegion;
	private BitmapTextureAtlas kazukiJuegoTextureAtlas;
	public TiledTextureRegion akaneJuegoRegion;
	private BitmapTextureAtlas akaneJuegoTextureAtlas;
	
	/* Enemigos */
	
	public TiledTextureRegion minionJuegoRegion;
	private BitmapTextureAtlas minionJuegoTextureAtlas;
	public TiledTextureRegion musashiJuegoRegion;
	private BitmapTextureAtlas musashiJuegoTextureAtlas;
	
	/* Primer Nivel */
	
	// Escenario
	public ITextureRegion casasLv1TextureRegion;	// Mas cercano
	private ITexture casasLv1Texture;
	public ITextureRegion montanasLv1TextureRegion;	// Medio
	private ITexture montanasLv1Texture;
	private ITexture cieloLv1Texture;				// Mas lejano
	public ITextureRegion cieloLv1TextureRegion;
	public ITextureRegion piedraLv1Region;
	private BitmapTextureAtlas piedraLv1TextureAtlas;
	
	/* Controladores y disparos */
	
	public ITextureRegion botonSaltarRegion;
	private BitmapTextureAtlas botonSaltarTextureAtlas;
	public ITextureRegion botonDispararRegion;
	private BitmapTextureAtlas botonDispararTextureAtlas;
	public ITextureRegion balaRegion;
	private BitmapTextureAtlas balaTextureAtlas;
	public ITextureRegion botonPausa1Region;
	private BitmapTextureAtlas botonPausa1TextureAtlas;
	public ITextureRegion botonPausa2Region;
	private BitmapTextureAtlas botonPausa2TextureAtlas;
	
	/* Segundo Nivel */
	
	public ITextureRegion casasLv2TextureRegion;	// Mas cercano
	private ITexture casasLv2Texture;
	public ITextureRegion montanasLv2TextureRegion;	// Medio
	private ITexture montanasLv2Texture;
	private ITexture cieloLv2Texture;				// Mas lejano
	public ITextureRegion cieloLv2TextureRegion;
	public TiledTextureRegion itemRegion;
	private BitmapTextureAtlas itemTextureAtlas;
	
	/* Tercer Nivel */
	
	public ITextureRegion fondoNivel3Region;
	private BitmapTextureAtlas fondoNivel3TextureAtlas;
	
	/* Fonts */

	//Constantes fuentes
	private static final int ANCHO_FUENTE = ConstantesManejadores.ANCHO_FUENTE;
	private static final int ALTO_FUENTE = ConstantesManejadores.ALTO_FUENTE;
	private static final int COLOR_RELLENO = ConstantesManejadores.COLOR_FUENTE;
	
	// Letra
	public ITexture fuenteTexture;
	public Font tipoLetra;
	
	// Musica
	public Music musicaFondo;
	
	
	/* 
	 * **************************************************************
	 *                    Metodos del manejador de recursos
	 * **************************************************************
	 */

	// Se utiliza al cargar el juego. Prepara el manejador de recursos 
	// y establece los parametros.
	public static void prepararRecursos(Engine engine, GameActivity gActivity,
			Camera camara, VertexBufferObjectManager mVerticesObjetos) {
		getInstance().engine = engine;
		getInstance().gActivity = gActivity;
		getInstance().camara = camara;
		getInstance().mVerticesObjetos = mVerticesObjetos;
	}

	//Getter del manejador de recursos!
	public static ManejadorRecursos getInstance() {
		return INSTANCIA;
	}

	/* Splash Screen */

	//Cargar los recursos de la Splash Screen
	public void cargarSplashScreen() {
		splashTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_LOGO, ALTO_LOGO, TextureOptions.BILINEAR);
		splashRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				splashTextureAtlas, gActivity, "Menus/logoTEC.png", 0, 0);
		splashTextureAtlas.load();
	}

	//Descargar los recursos de la Splash Screen
	public void descargarSplashScreen() {
		splashTextureAtlas.unload();
		splashRegion = null;
	}

	/* Logo del Juego */
	
	public void cargarRecursosLogo() {
		logoJuegoTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		logoJuegoRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				logoJuegoTextureAtlas, gActivity, "Menus/fondoLogoJuego.jpg", 0, 0);
		logoJuegoTextureAtlas.load();
	}
	
	public void descargarRecursosLogo() {
		logoJuegoTextureAtlas.unload();
		logoJuegoRegion = null;
	}
	
	
	/* Main Menu Screen */

	//Cargar los recursos del Main Menu
	public void cargarRecursosMenu() {
		mainMenuTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		mainMenuRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				mainMenuTextureAtlas, gActivity, "Menus/fondoMenu.png", 0, 0);
		mainMenuTextureAtlas.load();
	}

	//Descargar los recursos del Main Menu
	public void descargarRecursosMenu() {
		mainMenuTextureAtlas.unload();
		mainMenuRegion = null;
	}

	/* About Screen */

	//Cargar los recursos de la About Screen
	public void cargarRecursosAbout() {
		aboutTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		aboutRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				aboutTextureAtlas, gActivity, "Menus/fondoCreditos.png", 0, 0);
		aboutTextureAtlas.load();
	}

	//Descargar los recursos de la About Screen
	public void descargarRecursosAbout() {
		aboutTextureAtlas.unload();
		aboutRegion = null;
	}

	/* Instructions Screen */

	//Cargar los recursos de la Instructions Screen
	public void cargarRecursosInstructions() {
		instrucTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		instrucRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				instrucTextureAtlas, gActivity, "Menus/fondoInstruct.png", 0, 0);
		instrucTextureAtlas.load();
	}

	//Descargar los recursos de la Instructions Screen
	public void descargarRecursosInstructions() {
		instrucTextureAtlas.unload();
		instrucRegion = null;
	}

	/* Scores Screen */

	//Cargar los recursos de la Scores Screen
	public void cargarRecursosScores() {
		scoresTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		scoresRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				scoresTextureAtlas, gActivity, "Menus/fondoScores.png", 0, 0);
		scoresTextureAtlas.load();
	}

	//Descargar recursos de la Scores Screen
	public void descargarRecursosScores() {
		scoresTextureAtlas.unload();
		scoresRegion = null;
	}

	/* Settings Screen */

	//Cargar los recursos de la Settings Screen
	public void cargarRecursosSettings() {
		settingsTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		settingsRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				settingsTextureAtlas, gActivity, "Menus/fondoSettings.png", 0, 0);
		settingsTextureAtlas.load();
		cargarRecursosBotonesAudio();
	}

	//Descargar los recursos de la Settings Screen
	public void descargarRecursosSettings() {
		settingsTextureAtlas.unload();
		settingsRegion = null;
		descargarRecursosBotonesAudio();
	}

	//Cargar botones del audio: Estos botones se cargan aparte porque
	//nada mas se utilizan en esta pantalla, a diferencia de los otros que
	//aparecen en varias.
	private void cargarRecursosBotonesAudio() {

		// Boton Musica
		botonMusicaTextureAtlas = new BuildableBitmapTextureAtlas(
				gActivity.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
		botonMusicaRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(botonMusicaTextureAtlas, gActivity, "Menus/botonMusica.png", 2,	1);
		try {
			botonMusicaTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
		} catch (TextureAtlasBuilderException e) {
			Log.d("BUILDER", e.getMessage());
		}

		botonMusicaTextureAtlas.load();

		// Boton Sonido
		botonSonidosTextureAtlas = new BuildableBitmapTextureAtlas
				(gActivity.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
		botonSonidosRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(botonSonidosTextureAtlas, gActivity, "Menus/botonSonido.png", 2, 1);				
		try {
			botonSonidosTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
		} catch (TextureAtlasBuilderException e) {
			Log.d("BUILDER", e.getMessage());
		}

		botonSonidosTextureAtlas.load();
	}

	//Descargar los botones del audio
	private void descargarRecursosBotonesAudio() {
		botonMusicaTextureAtlas.unload();
		botonSonidosTextureAtlas.unload();
		
		botonMusicaRegion = null;
		botonSonidosRegion = null;
	}

	/* Loading Screen */

	// Cargar recursos de la pantalla loading
	public void cargarRecursosLoading() {
		loadingTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		loadingRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				loadingTextureAtlas, gActivity, "Menus/fondoCargando.png", 0, 0);
		loadingTextureAtlas.load();
	}

	// Descargar recursos de la pantalla loading
	public void descargarRecursosLoading() {
		loadingTextureAtlas.unload();
		loadingRegion = null;
	}

	/*
	 * Botones: Aqui se cargan y descargan los recursos de todos los botones.
	 * Estos metodos se llaman a la hora de crear las escenas en cada una.
	 */

	//Cargar los recursos de los botones
	public void cargarRecursosBotones() {

		//Boton de About
		botonAboutTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				100, 50, TextureOptions.BILINEAR);
		botonAboutRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonAboutTextureAtlas, gActivity, "Menus/botonAbout.png", 0, 0);
		botonAboutTextureAtlas.load();

		//Boton de Play
		botonPlayTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				100, 50, TextureOptions.BILINEAR);
		botonPlayRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonPlayTextureAtlas, gActivity, "Menus/botonPlay.png", 0, 0);
		botonPlayTextureAtlas.load();

		//Boton de Settings
		botonSettingsTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				100, 50, TextureOptions.BILINEAR);
		botonSettingsRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonSettingsTextureAtlas, gActivity, "Menus/botonSettings.png", 0, 0);
		botonSettingsTextureAtlas.load();

		//Boton de Scores
		botonScoresTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				110, 110, TextureOptions.BILINEAR);
		botonScoresRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonScoresTextureAtlas, gActivity, "Menus/botonScores.png", 0, 0);
		botonScoresTextureAtlas.load();

		//Boton de Instructions
		botonInstrucTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				110, 110, TextureOptions.BILINEAR);
		botonInstrucRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonInstrucTextureAtlas, gActivity, "Menus/botonInstructions.png", 0, 0);
		botonInstrucTextureAtlas.load();
	}

	//Descargar los recursos de los botones
	public void descargarRecursosBotones() {
		botonAboutTextureAtlas.unload();
		botonPlayTextureAtlas.unload();
		botonSettingsTextureAtlas.unload();
		botonScoresTextureAtlas.unload();
		botonInstrucTextureAtlas.unload();

		botonAboutRegion = null;
		botonPlayRegion = null;
		botonSettingsRegion = null;
		botonScoresRegion = null;
		botonInstrucRegion = null;
	}
	
	/* 
	 * **************************************************************
	 *                 Pre-Niveles (Eleccion de Personaje y Nivel)
	 * **************************************************************
	 */

	/* Elegir Personaje */

	//Cargar recursos escena Elegir Personaje
	public void cargarRecursosElegirPersonaje() {
		elegirPersonajeTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		elegirPersonajeRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				elegirPersonajeTextureAtlas, gActivity, "Menus/fondoElegirPersonaje.png", 0, 0);
		elegirPersonajeTextureAtlas.load();
		cargarRecursosBotonesPersonajes();
	}

	//Descargar recursos escena Elegir Personaje
	public void descargarRecursosElegirPersonaje() {
		elegirPersonajeTextureAtlas.unload();
		elegirPersonajeRegion = null;
		descargarRecursosBotonesPersonajes();
	}

	//Cargar botones para elegir personaje
	private void cargarRecursosBotonesPersonajes() {

		//Boton de Personaje Akane
		botonAkaneTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_BOTON_AKANE, ALTO_BOTON_AKANE, TextureOptions.BILINEAR);
		botonAkaneRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonAkaneTextureAtlas, gActivity, "Menus/AkanePersonaje.png", 0, 0);
		botonAkaneTextureAtlas.load();

		//Boton de Personaje Kazuki
		botonKazukiTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_BOTON_KAZUKI, ALTO_BOTON_KAZUKI, TextureOptions.BILINEAR);
		botonKazukiRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonKazukiTextureAtlas, gActivity, "Menus/KazukiPersonaje.png", 0, 0);
		botonKazukiTextureAtlas.load();
	}

	//Descargar botones para elegir personaje
	private void descargarRecursosBotonesPersonajes() {
		botonAkaneTextureAtlas.unload();
		botonKazukiTextureAtlas.unload();

		botonAkaneRegion = null;
		botonKazukiRegion = null;
	}

	/* Elegir Nivel */

	//Cargar recursos elegir nivel
	public void cargarRecursosElegirNivel() {
		elegirNivelTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		elegirNivelRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				elegirNivelTextureAtlas, gActivity, "Menus/fondoElegirNivel.png", 0, 0);
		elegirNivelTextureAtlas.load();
		cargarRecursosBotonesNiveles();
	}

	//Descargar recursos elegir nivel
	public void descargarRecursosElegirNivel() {
		elegirNivelTextureAtlas.unload();
		elegirNivelRegion = null;
		descargarRecursosBotonesNiveles();
	}

	//Cargar recursos botones niveles
	private void cargarRecursosBotonesNiveles() {

		// Boton de Nivel 1
		botonNivelUnoTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				BOTON_NIVELX, BOTON_NIVELX, TextureOptions.BILINEAR);
		botonNivelUnoRegion = BitmapTextureAtlasTextureRegionFactory.
				createFromAsset(botonNivelUnoTextureAtlas, gActivity, "Menus/botonNivel1.png", 0, 0);
		botonNivelUnoTextureAtlas.load();

		// Boton de Nivel 2
		botonNivelDosTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				BOTON_NIVELX, BOTON_NIVELX, TextureOptions.BILINEAR);
		botonNivelDosRegion = BitmapTextureAtlasTextureRegionFactory.
				createFromAsset(botonNivelDosTextureAtlas, gActivity, "Menus/botonNivel2.png", 0, 0);
		botonNivelDosTextureAtlas.load();

		// Boton de Nivel 3
		botonNivelTresTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				BOTON_NIVELX, BOTON_NIVELX, TextureOptions.BILINEAR);
		botonNivelTresRegion = BitmapTextureAtlasTextureRegionFactory.
				createFromAsset(botonNivelTresTextureAtlas, gActivity, "Menus/botonNivel3.png", 0, 0);
		botonNivelTresTextureAtlas.load();
		
		// Boton de Bloquear Nivel
		botonBloquearNivelAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(), 100, 100);
		botonBloquearNivelRegion = BitmapTextureAtlasTextureRegionFactory.
				createFromAsset(botonBloquearNivelAtlas, gActivity, "Menus/candado.png", 0, 0);
		botonBloquearNivelAtlas.load();
	}

	//Descargar recursos botones niveles
	private void descargarRecursosBotonesNiveles() {
		botonNivelUnoTextureAtlas.unload();
		botonNivelDosTextureAtlas.unload();
		botonNivelTresTextureAtlas.unload();
		botonBloquearNivelAtlas.unload();

		botonNivelUnoRegion = null;
		botonNivelDosRegion = null;
		botonNivelTresRegion = null;
		botonBloquearNivelRegion = null;
	}
	
	/* 
	 * **************************************************************
	 *                                   Estados de Nivel
	 * **************************************************************
	 */

	/* Nivel Superado */

	//Cargar recursos del nivel superado
	public void cargarRecursosGanaste() {
		fondoGanasteTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		fondoGanasteRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				fondoGanasteTextureAtlas, gActivity, "Menus/fondoNivelSuperado.png", 0, 0);
		fondoGanasteTextureAtlas.load();
		cargarRecursosBotonesEstado();
	}

	//Descargar recusos del nivel superado
	public void descargarRecursosGanaste() {
		fondoGanasteTextureAtlas.unload();
		fondoGanasteRegion = null;
		descargarRecursosBotonesEstado();
	}

	/* Perdiste Nivel */

	//Cargar recursos de pantalla perdiste
	public void cargarRecursosPerdiste() {
		fondoPerdisteTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		fondoPerdisteRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				fondoPerdisteTextureAtlas, gActivity, "Menus/fondoPerdiste.png", 0, 0);
		fondoPerdisteTextureAtlas.load();
		cargarRecursosBotonesEstado();
	}

	//Descargar recusos de pantalla perdiste
	public void descargarRecursosPerdiste() {
		fondoPerdisteTextureAtlas.unload();
		fondoPerdisteRegion = null;
		descargarRecursosBotonesEstado();
	}

	/* Pausa Nivel */

	//Cargar recursos de pantalla pausa
	public void cargarRecursosPausa() {
		fondoPausaTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		fondoPausaRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				fondoPausaTextureAtlas, gActivity, "Menus/fondoPausa.png", 0, 0);
		fondoPausaTextureAtlas.load();
		cargarRecursosBotonesEstado();
	}

	//Descargar recusos de pantalla pausa
	public void descargarRecursosPausa() {
		fondoPausaTextureAtlas.unload();
		fondoPausaRegion = null;
		descargarRecursosBotonesEstado();
	}

	/* Botones de Estado del Nivel */

	// Cargar los recursos de los botones de estado
	public void cargarRecursosBotonesEstado() {

		//Menu
		botonMenuTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_BOTON, ALTO_BOTON, TextureOptions.BILINEAR);
		botonMenuRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonMenuTextureAtlas, gActivity, "Menus/botonMenu.png", 0, 0);
		botonMenuTextureAtlas.load();

		//Volver a Jugar
		botonVolverJugarTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_BOTON, ALTO_BOTON, TextureOptions.BILINEAR);
		botonVolverJugarRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonVolverJugarTextureAtlas, gActivity, "Menus/botonVolverJugar.png", 0, 0);
		botonVolverJugarTextureAtlas.load();

		//Siguiente Nivel
		botonSigNivelTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_BOTON, ALTO_BOTON, TextureOptions.BILINEAR);
		botonSigNivelRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonSigNivelTextureAtlas, gActivity, "Menus/botonSigNivel.png", 0, 0);
		botonSigNivelTextureAtlas.load();
		
		//Reanudar
		botonReanudarTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_BOTON, ALTO_BOTON, TextureOptions.BILINEAR);
		botonReanudarRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonReanudarTextureAtlas, gActivity, "Menus/botonReanudar.png", 0, 0);
		botonReanudarTextureAtlas.load();
	}

	// Descargar los recursos de los botones de estado
	public void descargarRecursosBotonesEstado() {
		botonMenuTextureAtlas.unload();
		botonVolverJugarTextureAtlas.unload();
		botonSigNivelTextureAtlas.unload();
		botonReanudarTextureAtlas.unload();

		botonMenuRegion = null;
		botonVolverJugarRegion = null;
		botonSigNivelRegion = null;
		botonReanudarRegion = null;
	}
	
	/* 
	 * **************************************************************
	 *                                  Niveles
	 * **************************************************************
	 */
	
	/* Personajes y Enemigos */
	
	// Cargar recursos personajes
	public void cargarRecursosPersonajes() {
		
		// AKANE
		akaneJuegoTextureAtlas = new BitmapTextureAtlas
				(gActivity.getTextureManager(), 1089, 647, TextureOptions.DEFAULT);
		akaneJuegoRegion = BitmapTextureAtlasTextureRegionFactory.
				createTiledFromAsset(akaneJuegoTextureAtlas, gActivity, "PersonajesJuego/AkaneSprite2.png", 0, 0, 4, 2);
		akaneJuegoTextureAtlas.load();
		
		// KAZUKI
		kazukiJuegoTextureAtlas = new BitmapTextureAtlas
				(gActivity.getTextureManager(), 668, 495, TextureOptions.DEFAULT);
		kazukiJuegoRegion = BitmapTextureAtlasTextureRegionFactory.
				createTiledFromAsset(kazukiJuegoTextureAtlas, gActivity, "PersonajesJuego/KazukiSprite.png", 0, 0, 4, 2);
		kazukiJuegoTextureAtlas.load();
		
		
		// MINION
		minionJuegoTextureAtlas = new BitmapTextureAtlas
				(gActivity.getTextureManager(), 275, 298, TextureOptions.DEFAULT);
		minionJuegoRegion = BitmapTextureAtlasTextureRegionFactory.
				createTiledFromAsset(minionJuegoTextureAtlas, gActivity, "PersonajesJuego/MinionSprite.png", 0, 0, 2, 2);
		minionJuegoTextureAtlas.load();
		
		// MUSASHI
		musashiJuegoTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(), 582, 772);
		musashiJuegoRegion = BitmapTextureAtlasTextureRegionFactory.
				createTiledFromAsset(musashiJuegoTextureAtlas, gActivity, "PersonajesJuego/MushashiSP.png", 0, 0, 2, 2);
		musashiJuegoTextureAtlas.load();
		
		// BOUTICA
	}
	
	// Descargar recursos personajes
	public void descargarRecursosPersonajes() {
		akaneJuegoTextureAtlas.unload();
		kazukiJuegoTextureAtlas.unload();
		minionJuegoTextureAtlas.unload();
		musashiJuegoTextureAtlas.unload();
		
		akaneJuegoRegion = null;
		kazukiJuegoRegion = null;
		minionJuegoRegion = null;
		musashiJuegoRegion = null;
	}
	
	/* Elementos del escenario */
	
	// Cargar flechas y disparar
	private void cargarControlesJuego() {
		
		botonSaltarTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(), 75, 75);
		botonSaltarRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonSaltarTextureAtlas, gActivity, "Escenario/botonSaltar_cool.png", 0, 0);
		botonSaltarTextureAtlas.load();
		
		botonDispararTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(), 75, 75);
		botonDispararRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonDispararTextureAtlas, gActivity, "Escenario/botonDisparar_cool.png", 0, 0);
		botonDispararTextureAtlas.load();
		
		balaTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(), 50, 50);
		balaRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				balaTextureAtlas, gActivity, "Escenario/bala.png", 0, 0);
		balaTextureAtlas.load();
		
		itemTextureAtlas = new BitmapTextureAtlas
				(gActivity.getTextureManager(), 900, 900, TextureOptions.DEFAULT);
		itemRegion = BitmapTextureAtlasTextureRegionFactory.
				createTiledFromAsset(itemTextureAtlas, gActivity, "Escenario/explosion.png", 0, 0, 9, 9);
		itemTextureAtlas.load();
		
		botonPausa1TextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(), 50, 50);
		botonPausa1Region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonPausa1TextureAtlas, gActivity, "Escenario/botonPausa1.png", 0, 0);
		botonPausa1TextureAtlas.load();
		
		botonPausa2TextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(), 50, 50);
		botonPausa2Region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonPausa2TextureAtlas, gActivity, "Escenario/botonPausa2.png", 0, 0);
		botonPausa2TextureAtlas.load();
		
		botonReanudarTextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_BOTON, ALTO_BOTON, TextureOptions.BILINEAR);
		botonReanudarRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				botonReanudarTextureAtlas, gActivity, "Menus/botonReanudar.png", 0, 0);
		botonReanudarTextureAtlas.load();
	}
	
	// Descargar flechas y disparar
	private void descargarControlesJuego() {
		botonSaltarTextureAtlas.unload();
		botonDispararTextureAtlas.unload();
		balaTextureAtlas.unload();
		itemTextureAtlas.unload();
		botonPausa1TextureAtlas.unload();
		botonReanudarTextureAtlas.unload();
		
		botonSaltarRegion = null;
		botonDispararRegion = null;
		balaRegion = null;
		itemRegion = null;
		botonPausa1Region = null;
		botonReanudarRegion = null;
	}
	
	/* Nivel 1 */
	
	// Cargar recursos nivel 1
	public void cargarRecursosNivel1() {
		cargarFondoNivel1();
		cargarRecursosPersonajes();
		cargarControlesJuego();
	}

	// Descargar recursos nivel 1
	public void descargarRecursosNivel1() {
		descargarFondoNivel1();
		descargarRecursosPersonajes();
		descargarControlesJuego();
	}
	
	// Cargar fondo nivel 1
	private void cargarFondoNivel1() {
		
		piedraLv1TextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(), 75, 75);
		piedraLv1Region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				piedraLv1TextureAtlas, gActivity, "Nivel1/piedraCool.png", 0, 0);
		piedraLv1TextureAtlas.load();
		
		// Carga la imagen del fondo mas lejano
		try {
			cieloLv1Texture = new AssetBitmapTexture(
					gActivity.getTextureManager(), gActivity.getAssets(), "Nivel1/cieloNivel1.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		cieloLv1TextureRegion = TextureRegionFactory
				.extractFromTexture(cieloLv1Texture);
		cieloLv1Texture.load();
		
		// Carga la imagen del fondo medio
		try {
			montanasLv1Texture = new AssetBitmapTexture(
					gActivity.getTextureManager(), gActivity.getAssets(), "Nivel1/montanasNivel1.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		montanasLv1TextureRegion = TextureRegionFactory
				.extractFromTexture(montanasLv1Texture);
		montanasLv1Texture.load();
		
		// Carga la imagen mass cercana
		try {
			casasLv1Texture = new AssetBitmapTexture(
					gActivity.getTextureManager(), gActivity.getAssets(), "Nivel1/casasNivel1.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		casasLv1TextureRegion = TextureRegionFactory
				.extractFromTexture(casasLv1Texture);
		casasLv1Texture.load();
	}
	
	private void descargarFondoNivel1() {
		cieloLv1Texture.unload();
		montanasLv1Texture.unload();
		casasLv1Texture.unload();
		piedraLv1TextureAtlas.unload();
		
		cieloLv1TextureRegion = null;
		montanasLv1TextureRegion = null;
		casasLv1TextureRegion = null;
		piedraLv1Region = null;
	}
	
	/* Nivel 2 */
	
	// Cargar recursos nivel2
	public void cargarRecursosNivel2() {
		cargarFondoNivel2();
		cargarRecursosPersonajes();
		cargarControlesJuego();
	}

	// Descargar recursos nivel 2
	public void descargarRecursosNivel2() {
		descargarFondoNivel2();
		descargarRecursosPersonajes();
		descargarControlesJuego();
	}
	
	// Cargar fondo nivel 2
	private void cargarFondoNivel2() {
		
		// Carga la imagen del fondo mas lejano
		try {
			cieloLv2Texture = new AssetBitmapTexture(
					gActivity.getTextureManager(), gActivity.getAssets(), "Nivel2/cieloNivel2.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		cieloLv2TextureRegion = TextureRegionFactory
				.extractFromTexture(cieloLv2Texture);
		cieloLv2Texture.load();
		
		// Carga la imagen del fondo medio
		try {
			montanasLv2Texture = new AssetBitmapTexture(
					gActivity.getTextureManager(), gActivity.getAssets(), "Nivel2/montanasNivel2.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		montanasLv2TextureRegion = TextureRegionFactory
				.extractFromTexture(montanasLv2Texture);
		montanasLv2Texture.load();
		
		// Carga la imagen mass cercana
		try {
			casasLv2Texture = new AssetBitmapTexture(
					gActivity.getTextureManager(), gActivity.getAssets(), "Nivel2/PisoCool.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		casasLv2TextureRegion = TextureRegionFactory
				.extractFromTexture(casasLv2Texture);
		casasLv2Texture.load();
	}
	
	private void descargarFondoNivel2() {
		cieloLv2Texture.unload();
		montanasLv2Texture.unload();
		casasLv2Texture.unload();
		
		cieloLv2TextureRegion = null;
		montanasLv2TextureRegion = null;
		casasLv2TextureRegion = null;
	}
	
	
	/* Nivel 3 */
	
	public void cargarRecursosNivel3() {
		
		fondoNivel3TextureAtlas = new BitmapTextureAtlas(gActivity.getTextureManager(),
				ANCHO_MENU, ALTO_MENU, TextureOptions.BILINEAR);
		fondoNivel3Region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				fondoNivel3TextureAtlas, gActivity, "Nivel3/fondoNivelPrueba.png", 0, 0);
		fondoNivel3TextureAtlas.load();
	}
	
	public void descargarRecursosNivel3() {
		fondoNivel3TextureAtlas.unload();
		fondoNivel3Region = null;
	}
	
	/* Fuentes para marcadores, puntajes... */
	
	// Cargar fuentes
	public void cargarRecursosFuentes() {
		
	    fuenteTexture = new BitmapTextureAtlas(gActivity.getTextureManager(),
	    		ANCHO_FUENTE, ALTO_FUENTE, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
	    tipoLetra = FontFactory.createFromAsset(gActivity.getFontManager(), fuenteTexture,
	    		gActivity.getAssets(), "Fonts/Ginjah_Ninjah.ttf", 32f, true, COLOR_RELLENO);
		fuenteTexture.load();
	}
	
	// Descargar fuentes
	public void descargarRecursosFuentes() {
		fuenteTexture.unload();
		tipoLetra = null;
	}
}